import GCConnection from "../../GCConnection";
import BaseWindow from "../common-components/BaseWindow";
import GCBlock from "../common-components/GCBlock";
import GCToast from "../common-components/GCToast";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu('wnds/TipComp')
export default class TipComp extends BaseWindow {

    @property(cc.Label)
    tipLbl: cc.Label = null;

    private _tip: string = '';

    get tip() { return this._tip }

    protected onLoad(): void {
        this.tipLbl.string = '';
    }

    initUI(tip: string) {
        this._tip = tip;
        this.tipLbl.string = tip;
    }

    protected onEnable(): void {
        super.onEnable();

        if (this._tip) {
            this.tipLbl.string = this._tip;
            return;
        }

        this.requestTip();
    }

    protected async requestTip() {
        GCBlock.block();

        try {
            const resp = await GCConnection.requestTip();


            if (!cc.isValid(this.node)) return;
            this.initUI(resp.tip);
        } catch (e) {
            GCToast.toast('获取温馨提示失败');
        } finally {
            GCBlock.unblock();
        }
    }
}
